import { notification, message } from 'antd';
import cloneDeep from 'lodash/cloneDeep';
import _map from 'lodash/map';
import _uniq from 'lodash/uniq';
import _pull from 'lodash/pull';
import { GetDateThree, FormatnumberZero } from '../../../../function';
import {
  getHistoryByType,
  getRunPoints,
  getStockHistoryRunPoint,
} from '../../../../services/allposition_service';

export default {
  namespace: 'runpoints',
  state: {
    stocks: { code: 0, message: '', list: [] },
    stocksFilters: {},
  },

  effects: {
    // stock
    *GetRunPoints({ payload, callback }, { call, put }) {
      const response = yield call(getRunPoints, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        yield put({
          type: 'saveStocks',
          payload: {
            list: { code: 1, message: response.message, list: response.list },
            filter: {},
          },
        });
        if (callback) callback(response);
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },

    *GetStockHistoryRunPoint({ payload, callback }, { call, put }) {
      const response = yield call(getStockHistoryRunPoint, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        const res = { list: [] };
        const { list } = response.list;
        for (let j = 0; j < list.length; j++) {
          if (payload.betaFlag) {
            res.list.push({
              type: '涨跌幅',
              x: (list[j].runTime || '').split(' ')[0],
              value: list[j].percent,
            });
            res.list.push({
              type: 'SPY涨跌幅',
              x: (list[j].runTime || '').split(' ')[0],
              value: list[j].spyPercent,
            });
            res.list.push({
              type: '跑赢SPY比率(1周）/Beta',
              x: (list[j].runTime || '').split(' ')[0],
              value: list[j].betaRateOneWeek,
            });
            res.list.push({
              type: '跑赢SPY比率(2周）/Beta',
              x: (list[j].runTime || '').split(' ')[0],
              value: list[j].betaRateTwoWeek,
            });
            res.list.push({
              type: '跑赢SPY比率(1月）/Beta',
              x: (list[j].runTime || '').split(' ')[0],
              value: list[j].betaRateOneMonth,
            });
            res.list.push({
              type: '跑赢SPY比率(3月）/Beta',
              x: (list[j].runTime || '').split(' ')[0],
              value: list[j].betaRateThreeMonth,
            });
            res.list.push({
              type: '跑赢SPY比率(6月）/Beta',
              x: (list[j].runTime || '').split(' ')[0],
              value: list[j].betaRateSixMonth,
            });
            res.list.push({
              type: '跑赢SPY比率(1年）/Beta',
              x: (list[j].runTime || '').split(' ')[0],
              value: list[j].betaRateOneYear,
            });
          } else {
            res.list.push({
              type: '涨跌幅',
              x: (list[j].runTime || '').split(' ')[0],
              value: list[j].percent,
            });
            res.list.push({
              type: 'SPY涨跌幅',
              x: (list[j].runTime || '').split(' ')[0],
              value: list[j].spyPercent,
            });
            res.list.push({
              type: '跑赢SPY比率(1周）',
              x: (list[j].runTime || '').split(' ')[0],
              value: list[j].rateOneWeek,
            });
            res.list.push({
              type: '跑赢SPY比率(2周）',
              x: (list[j].runTime || '').split(' ')[0],
              value: list[j].rateTwoWeek,
            });
            res.list.push({
              type: '跑赢SPY比率(1月）',
              x: (list[j].runTime || '').split(' ')[0],
              value: list[j].rateOneMonth,
            });
            res.list.push({
              type: '跑赢SPY比率(3月）',
              x: (list[j].runTime || '').split(' ')[0],
              value: list[j].rateThreeMonth,
            });
            res.list.push({
              type: '跑赢SPY比率(6月）',
              x: (list[j].runTime || '').split(' ')[0],
              value: list[j].rateSixMonth,
            });
            res.list.push({
              type: '跑赢SPY比率(1年）',
              x: (list[j].runTime || '').split(' ')[0],
              value: list[j].rateOneYear,
            });
          }
        }
        if (callback) callback(res);
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },
  },

  reducers: {
    saveStocks(state, action) {
      return {
        ...state,
        stocks: action.payload.list,
        stocksFilters: action.payload.filter,
      };
    },
  },
};
